package redisex

import (
	"context"
	"github.com/redis/go-redis/v9"
	"sync"
)

var once sync.Once
var client *redis.Client

type Config struct {
	Addr string `mapstructure:"addr" json:"addr"`
	Db   int    `mapstructure:"db" json:"db"`
}

func GetClient() *redis.Client {
	return client
}

func InitRedisClient(config Config) {
	once.Do(func() {
		client = redis.NewClient(&redis.Options{
			Addr: config.Addr,
			DB:   config.Db,
		})
		result := client.Ping(context.Background())
		if result.Err() != nil {
			panic(result.Err())
		}
	})
}
